package com.admin.web.controller;

import com.admin.web.model.AjaxJson;
import com.admin.web.verify.ParamVerify;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.classify.Classify;
import com.bayss.core.entity.classify.ClassifyItem;
import com.bayss.core.enums.ClassifyType;
import com.bayss.core.service.ClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 规格管理
 *
 * @author helen
 */
@RestController
public class ClassifyController extends ApiBaseController {
    @Autowired
    private ClassifyService classifyService;

    /**
     * 创建规格
     * @param classify
     * @return
     */
    @RequestMapping(value = "/classify/add.json", method = RequestMethod.POST)
    public ResultMsg addClassify(Classify classify) {
        try {
            return classifyService.createClassify(classify);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1,"创建失败");
        }
    }

    /**
     * 删除规格
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/classify/delete.json", method = RequestMethod.POST)
    public ResultMsg deleteClassify(@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {
        try {
            classifyService.deleteClassify(id);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "删除失败");
        }
    }

    /**
     * 根据id查询规格
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/classify/get.json", method = RequestMethod.GET)
    public AjaxJson getClassify(@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            Classify classifyById = classifyService.findClassifyById(id);
            ajaxJson.setStatus(200).setMsg("获取成功").setData(classifyById);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("获取失败");
        }
        return ajaxJson;
    }

    /**
     * 更新规格
     *
     * @param classify
     * @return
     */
    @RequestMapping(value = "/classify/update.json", method = RequestMethod.POST)
    public AjaxJson updateClassify(Classify classify) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            int result = classifyService.updateClassify(classify);
            ajaxJson.setStatus(200).setMsg("更新成功").setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("更新失败");
        }
        return ajaxJson;
    }

    /**
     * 规格查询不分页
     *
     * @param classifyNameCn
     * @return
     */
    @RequestMapping(value = "/classify/list.json", method = RequestMethod.GET)
    public ResultMsg listClassify(@RequestParam(name = "classifyNameCn", required = false, defaultValue = "") String classifyNameCn) {
        return ResultMsg.success(classifyService.findClassifyList(null));
    }

    /**
     * 自选配料
     */
    @RequestMapping(value = "/classify/ingredients.json", method = RequestMethod.GET)
    public ResultMsg optionalClassify() {
        return ResultMsg.success(classifyService.findClassifyList(ClassifyType.INGREDIENTS.getType()));
    }
    /**
     * 规格分页查询
     */
    @RequestMapping(value = "/classify/listPage.json", method = RequestMethod.GET)
    public ResultMsg listPage(Integer page, Integer pageSize) {
        return ResultMsg.success(classifyService.findByPage(page, pageSize));
    }

    /**
     * 添加规格明细
     *
     * @param classifyItem
     * @return
     */
    @RequestMapping(value = "/classify/item/add.json", method = RequestMethod.POST)
    public ResultMsg addClassifyItem(ClassifyItem classifyItem) {
        try {
            return classifyService.createClassifyItem(classifyItem);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250,"提交失败");
        }
    }

    /**
     * 更新规格明细
     *
     * @param classifyItem
     * @return
     */
    @RequestMapping(value = "/classify/item/update.json", method = RequestMethod.POST)
    public AjaxJson updateClassifyItem(ClassifyItem classifyItem) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            int result = classifyService.updateClassifyItem(classifyItem);
            ajaxJson.setStatus(200).setMsg("更新成功").setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("更新失败");
        }
        return ajaxJson;
    }

    /**
     * 删除规格明细
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/classify/item/delete.json", method = RequestMethod.POST)
    public AjaxJson deleteClassifyItem(@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {

        AjaxJson ajaxJson = new AjaxJson();
        try {
            int result = classifyService.deleteClassifyItem(id);
            ajaxJson.setStatus(200).setMsg("删除成功").setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("删除失败");
        }
        return ajaxJson;
    }

    /**
     * 查询规格明细
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/classify/item/get.json", method = RequestMethod.POST)
    public AjaxJson getClassifyItem(@RequestParam(value = "id", defaultValue = "", required = false) Integer id) {

        AjaxJson ajaxJson = new AjaxJson();
        try {
            ClassifyItem classifyItemById = classifyService.findClassifyItemById(id);
            ajaxJson.setStatus(200).setMsg("查询成功").setData(classifyItemById);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("删除失败");
        }
        return ajaxJson;
    }

    /**
     * 查询所有规格明细
     *
     * @param classifyId
     * @return
     */
    @RequestMapping(value = "/classify/item/list.json", method = RequestMethod.POST)
    public AjaxJson listClassifyItem(@RequestParam(value = "classify_id") Integer classifyId) {

        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<ClassifyItem> classifyItemByClassifyId = classifyService.findClassifyItemByClassifyId(classifyId);
            ajaxJson.setStatus(200).setMsg("查询成功").setData(classifyItemByClassifyId);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("删除失败");
        }
        return ajaxJson;
    }

}
